package com.wulinbus.controller.admin;

import java.io.IOException;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.fancy.common.utils.StringUtil;
import com.wulinbus.model.CreativeHelperModel;
import com.wulinbus.model.ScriptBannerModel;
import com.wulinbus.service.CreativeHelperService;
import com.wulinbus.utils.AdminAuthCheck;
@Controller
@AdminAuthCheck
@RequestMapping("/admin/creativeHelper")
public class AdminCreativeHelperController {
	@Resource
	private CreativeHelperService creativeHelperService;
	
	@RequestMapping("/add.html")
	public String add(@CookieValue(value="muid",defaultValue="") String muid,Model model) {
		if(StringUtil.isEmpty(muid))
		{
			return "admin/login";
		}
		else
		{
		 	model.asMap().clear();
		 	return "admin/creativeHelper_add";
		}
	}
	@RequestMapping("/editor.html")
	public String editor(Integer hid,@CookieValue(value="muid",defaultValue="") String muid,Model model) {
		if(StringUtil.isEmpty(muid))
		{
			return "admin/login";
		}
		else
		{
			model.asMap().clear();
			CreativeHelperModel creativeHelperModel=creativeHelperService.getCreativeHelperByHid(hid);
			model.addAttribute("creativeHelperModel", creativeHelperModel);
			return "admin/creativeHelper_editor";
		}
	}
	/**
	 * 创意助手添加
	 */
	@RequestMapping(value = "/add.json", method = { RequestMethod.POST })
	public void add_post(Model model,CreativeHelperModel creativeHelper, HttpServletRequest request, HttpServletResponse response) throws IOException {
		response.setContentType("application/json; charset=utf-8");
		creativeHelperService.insert(creativeHelper);
		response.getWriter().print("{\"message\":\"操作成功\",\"statusCode\":\"200\"}");
	}
	/**
	 * 创意助手编辑
	 */
	@RequestMapping(value = "/creative_helper_editor.json", method = { RequestMethod.POST })
	public void creative_helper_editor(Integer hid,Model model,CreativeHelperModel creativeHelper, HttpServletRequest request, HttpServletResponse response) throws IOException {
		response.setContentType("application/json; charset=utf-8");
		creativeHelper.setHid(hid);
		creativeHelperService.update(creativeHelper);
		response.getWriter().print("{\"message\":\"操作成功\",\"statusCode\":\"200\"}");
	}
	/**
	 * 创意助手删除
	 */
	@RequestMapping(value = "/delete.json", method = { RequestMethod.POST })
	public void delete(Model model,Integer hid, HttpServletRequest request, HttpServletResponse response) throws IOException {
		response.setContentType("application/json; charset=utf-8");
		if(creativeHelperService.delete(hid)>0){
			response.getWriter().print("{\"message\":\"操作成功\",\"statusCode\":\"200\"}");
		}
		else
		{
			response.getWriter().print("{\"message\":\"操作失败\",\"statusCode\":\"300\"}");
		}
	}
	
}
